<template>
	<view class="marquee">
		<view class="marquee-box" :class="direction" :style="{ animationDuration: duration + 's' }">
			<view class="marquee-list">
				<view class="item" v-for="(item, index) in list" :key="index">
					<image :src="item.url" :style="{
						width: width + 'rpx',
						height: height + 'rpx',
						marginRight: space + 'rpx'
					}" mode="heightFix" @tap="onClick(item)" lazy-load/>
				</view>
			</view>
			<view class="marquee-list">
				<view class="item" v-for="(item, index) in list" :key="index">
					<image :src="item.url" :style="{
						width: width + 'rpx',
						height: height + 'rpx',
						marginRight: space + 'rpx'
					}" mode="heightFix" @tap="onClick(item)"  lazy-load/>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	name: 'dcd-picmarquee',
	props: {
		list: {
			type: Array,
			default() {
				return []
			}
		},
		width: {
			type: Number,
			default: 300
		},
		height: {
			type: Number,
			default: 200
		},
		space: {
			type: Number,
			default: 20
		},
		duration: {
			type: Number,
			default: 20
		},
		direction: {
			type: String,
			default: 'left', // left 或 right
			validator: function (value) {
				return ['left', 'right'].indexOf(value) !== -1
			}
		}
	},
	methods: {
		onClick(item) {
			this.$emit('click', item)
		}
	}
}
</script>

<style>
.marquee {
	width: 100%;
	overflow: hidden;
	position: relative;
}

.marquee-box {
	display: flex;
	animation: marqueeLeft linear infinite;
	width: fit-content;
}

.marquee-box.right {
	animation-name: marqueeRight;
}

.marquee-box.left {
	animation-name: marqueeLeft;
}

.marquee-list {
	display: flex;
	flex-shrink: 0;
}

.item {
	display: flex;
}

.item image {
	display: block;
	border-radius: 12rpx;
}

@keyframes marqueeLeft {
	0% {
		transform: translateX(0);
	}

	100% {
		transform: translateX(-50%);
	}
}

@keyframes marqueeRight {
	0% {
		transform: translateX(-50%);
	}

	100% {
		transform: translateX(0);
	}
}
</style>